//
//  testViewController.m
//  BrickBreaker
//
//  Created by iD Student on 7/13/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "testViewController.h"

@interface testViewController ()

@end

@implementation testViewController

- (void)viewDidLoad {
    position = CGPointMake(15.0, 7.5);
    ball = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"apple.gif"]];
    ball.frame = CGRectMake(160, 240, 32, 32);
    [self.view addSubview:ball];
    timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(animate) userInfo:nil repeats:YES];

    
    
    [super viewDidLoad];

}

- (void)viewDidUnload
{
    brickLeft = nil;
    brickMiddle = nil;
    brickRight = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

-(void) animate
{
    if(CGRectIntersectsRect(ball.frame, paddle.frame))
    {
        position.y = -position.y;
    }
    ball.center = CGPointMake(ball.center.x + position.x, ball.center.y + position.y);
    if (ball.center.x > 288 || ball.center.x < 32)
       position.x = -position.x;
    if (ball.center.y < 20)
       position.y = -position.y;
    if (CGRectIntersectsRect(ball.frame,brickLeft.frame))
    { 
        brickLeft.hidden=TRUE;
        brickLeft=nil;
    }
    if (CGRectIntersectsRect(ball.frame, brickMiddle.frame)) {
        brickMiddle.hidden=TRUE;
        brickMiddle=nil;
    }
    if (CGRectIntersectsRect(ball.frame, brickRight.frame)) {
        brickRight.hidden=TRUE;
        brickRight=nil;
    }
}


-(void)moveLeft:(id)sender
{
    if(paddle.center.x > 50)
        paddle.center = CGPointMake(paddle.center.x - 20, paddle.center.y);
}

-(void)moveRight:(id)sender
{
    if(paddle.center.x < 270)
        paddle.center = CGPointMake(paddle.center.x + 20, paddle.center.y);
}



@end
